package bean.admin.manger;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.sql.ResultSet;

public class AdminMangerBean {
    int code;
    String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    public String mangePark(String id,String token,String tel,String nums,String price,String address,String image,
                            String introduction,String name){
        JDBCConnection jdbc = new JDBCConnection();
        String tokenArry[] = token.split("#");
        try{
            if(checkPermissions(id,tokenArry[0],tokenArry[2],jdbc)){
                if(!"".equals(tel)&&tel!=null)  jdbc.executeUpdate("UPDATE parkinglot set lottel=\""+tel+
                        "\" WHERE lotid=\""+id+"\";");
                if(!"".equals(nums)&&nums!=null)  jdbc.executeUpdate("UPDATE parkinglot set lotnums="+nums+
                        " WHERE lotid=\""+id+"\";");
                if(!"".equals(price)&&price!=null)  jdbc.executeUpdate("UPDATE parkinglot set price=\""+price+
                        "\" WHERE lotid=\""+id+"\";");
                if(!"".equals(address)&&address!=null)  jdbc.executeUpdate("UPDATE parkinglot set lotaddress=\""
                        +address+ "\" WHERE lotid=\""+id+"\";");
                if(!"".equals(image)&&image!=null)  jdbc.executeUpdate("UPDATE parkinglot set lotimage=\""
                        +image+ "\" WHERE lotid=\""+id+"\";");
                if(!"".equals(introduction)&&introduction!=null)  jdbc.executeUpdate("UPDATE parkinglot set lotintroduction=\""
                        +introduction+ "\" WHERE lotid=\""+id+"\";");
                if(!"".equals(name)&&name!=null)  jdbc.executeUpdate("UPDATE parkinglot set lotname=\""
                        +name+ "\" WHERE lotid=\""+id+"\";");
                code = 0;
                message = "成功";
            }
            else {
                code = 200;
                message = "token错误或者用户无权限";
            }
        }catch (Exception e){
            code = 100;
            message = "token错误";
        }
        return JSON.toJSONString(this);
    }
    private boolean checkPermissions(String park_id, String admin_id, String admin_pwd, JDBCConnection jdbc){
        String querySQL = "select lotid,adminpsd from lotmanager where adminid = '"+admin_id+"'";
        ResultSet rs = jdbc.executeQuery(querySQL);
        try {
            rs.next();
            if(park_id.equals(rs.getString("lotid"))&&
                    admin_pwd.equals(rs.getString("adminpsd"))) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
